/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.CustomerController;
import controller.GnomeController;
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;
import model.Gnome;
import model.GnomeType;
import model.Navigation;

/**
 *
 * @author zno
 */
@Named(value = "gnomeManager")
@ConversationScoped
public class GnomeManager implements Serializable {

    @EJB
    private GnomeController controller;

    @Inject
    private Conversation conversation;
    
    private ArrayList<Gnome> shoppingCart = new ArrayList<Gnome>();
    private ArrayList<Gnome> inventory = new ArrayList<Gnome>();
    private ArrayList<GnomeType> gnomeTypes= new ArrayList<GnomeType>();
    
    private ArrayList<Gnome> customersGnomes;
    
    private String gnomeTypeName;
    private String gnomeTypePrice;
    
    private String totalAmount;
    private boolean hasPaid;
    
    private boolean checkout;
    private boolean logout;
    
    private String name;
    private String password;
    
    public GnomeManager() 
    {
        
    }
    /**
    * Initiates a list containing all the gnomes that currently are for sale.
    *  
    */
    public void listInventory () {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        startConversation();
        inventory = controller.getInventory();
    }
    /**
     * Inititiates the list of the currently logged in customers gnomes.
     */
    public void listCustomersGnomes () {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        customersGnomes = controller.getCustomersGnomes(getName(),getPassword());
    }
    /*
     * Adds the selected gnome to the customers cart.
     * 
     * @param gnome     the selected gnome
     */
    public void addGnomeToCart (Gnome gnome) {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        inventory.remove(gnome);
        shoppingCart.add(gnome);
    }
    /**
     * Removes the selected gnome from the customers cart.
     * 
     * @param gnome the selected gnome
     */
    public void removeGnomeFromCart (Gnome gnome) {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        shoppingCart.remove(gnome);
    }
    /**
     * Calculates the total cost of the shopping cart and displays it in the view.
     */
    public void showTotalCost () {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        totalAmount = ""+controller.getTotalCost(shoppingCart);
    }
    /**
     * Pays for the cart by subtracting the total cost from the customers balance,
     * as well as adding the purchased gnomes to the customers own gnome collection.
     * 
     * The purchased gnomes are also marked as no longer being for sale.
     */
    public void payForCart () {
        startConversation();  
        setCheckout(false);
        setLogout(false);
        
        if (controller.payForCart(shoppingCart,getName(),getPassword()))
            shoppingCart.clear();
    }
    /**
     * Lists all the different types of gnomes that currently reside in the DB.
     */
    public void listGnomeTypes () {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        gnomeTypes = controller.getGnomeTypes();
    }
    /**
     * Creates a new gnome type
     * 
     * pseudo-param gnomeTypeName     The gnome type
     * pseudo-param gnomeTypePrice    The price for this gnome type
     */
    public void createGnomeType () {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        if (gnomeTypeName.length()>0 && gnomeTypePrice.length()>0) {
            gnomeTypes.add(controller.createGnomeType(gnomeTypeName,gnomeTypePrice));
        }
    }
    /**
     * Creates a gnome of a certain type
     * 
     * @param type      The selected gnome type that the gnome will be based on
     */
    public void createGnomeWithType (GnomeType type) {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        inventory.add(controller.createGnomeWithType(type));
    }
    /**
     * Kills a gnome
     * 
     * @param gnome     The selected gnome to be hanged
     */
    public void deleteGnome (Gnome gnome) {
        startConversation();
        setCheckout(false);
        setLogout(false);
        
        inventory = controller.deleteGnome(gnome);
    }
    /*
     * Retrieves the GnomeController
     * @return the gnomeController
     */
    public GnomeController getController() {
        return controller;
    }
    /**
     * Starts the conversation scope
     */
    private void startConversation() 
    {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    /**
     * Ends the conversation scope
     */
    private void stopConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }
    /**
     * Sets a new GnomeController.
     * @param controller 
     */
    public void setController(GnomeController controller) {
        this.controller = controller;
    }
    /*
     * Retrieves the current conversation scope
     * @return the current conversation scope
     */
    public Conversation getConversation() {
        return conversation;
    }
    /**
     * Sets a new conversation scope reference
     * @param conversation 
     */
    public void setConversation(Conversation conversation) {
        this.conversation = conversation;
    }
    /**
     * 
     * @return the current gnome inventory
     */
    public ArrayList<Gnome> getInventory() {
        return inventory;
    }
    /**
     * Sets a new gnome inventory.
     * @param inventory 
     */
    public void setInventory(ArrayList<Gnome> inventory) {
        this.inventory = inventory;
    }
    /**
     * 
     * @return the customers shoppingCart
     */
    public ArrayList<Gnome> getShoppingCart() {
        return shoppingCart;
    }
    /**
     * Sets a new shoppingCart
     * @param shoppingCart 
     */
    public void setShoppingCart(ArrayList<Gnome> shoppingCart) {
        this.shoppingCart = shoppingCart;
    }
    /**
     * 
     * @return the current list of gnome types 
     */
    public ArrayList<GnomeType> getGnomeTypes() {
        return gnomeTypes;
    }
    /**
     * Sets a new list of gnome types
     * @param gnomeTypes 
     */
    public void setGnomeTypes(ArrayList<GnomeType> gnomeTypes) {
        this.gnomeTypes = gnomeTypes;
    }
    /**
     * Used for creating a new gnome type
     * @return the user defined type of gnome 
     */
    public String getGnomeTypeName() {
        return gnomeTypeName;
    }
    /**
     * Used for creating a new gnome type
     * @param gnomeTypeName     the user defined type of gnome
     */
    public void setGnomeTypeName(String gnomeTypeName) {
        this.gnomeTypeName = gnomeTypeName;
    }
    /**
     * Used for creating a new gnome type
     * @return  the price of the new gnome type
     */
    public String getGnomeTypePrice() {
        return gnomeTypePrice;
    }
    /**
     * Sets the price for a type of gnome
     * @param gnomeTypePrice 
     */
    public void setGnomeTypePrice(String gnomeTypePrice) {
        this.gnomeTypePrice = gnomeTypePrice;
    }
    /**
     * 
     * @return  the total calculated amount of the current shopping cart. 
     */
    public String getTotalAmount() {
        return totalAmount;
    }
    /**
     * Sets the total amount for the current shopping cart.
     * @param totalAmount 
     */
    public void setTotalAmount(String totalAmount) {
        this.totalAmount = totalAmount;
    }
    /**
     * 
     * @return the status of payment
     */
    public boolean isHasPaid() {
        return hasPaid;
    }
    /*
     * @param hasPaid   sets the current status of payment
     */
    public void setHasPaid(boolean hasPaid) {
        this.hasPaid = hasPaid;
    }
    /**
     * 
     * @return  the current list of the customers purchased gnomes 
     */
    public ArrayList<Gnome> getCustomersGnomes() {
        return customersGnomes;
    }
    /**
     * @param customersGnomes   set the current customers list of purchased gnomes
     */
    public void setCustomersGnomes(ArrayList<Gnome> customersGnomes) {
        this.customersGnomes = customersGnomes;
    }
    /**
     * 
     * @return the current checkout status 
     */
    public boolean isCheckout() {
        return Navigation.getSingletonObject().isCheckout();
    }
    /**
     * 
     * @param checkout sets the current checkout status
     */
    public void setCheckout(boolean checkout) {
        Navigation.getSingletonObject().setCheckout(checkout);
    }
    /**
     * 
     * @return the current logout status 
     */
    public boolean isLogout() {
        return Navigation.getSingletonObject().isLogout();
    }
    /**
     * 
     * @param logout sets the current logout status
     */
    public void setLogout(boolean logout) {
        Navigation.getSingletonObject().setLogout(logout);
    }
    /**
     * Used for logging in and creating new Customers
     * @return the user defined Customer name
     */
    public String getName() {
        return Navigation.getSingletonObject().getName();
    }
    /**
     * Used for logging in and creating new Customers
     * @param name sets the Customer name
     */
    public void setName(String name) {
        Navigation.getSingletonObject().setName(name);
    }
    /**
     * Used for logging in.
     * @return the user defined password  
     */
    public String getPassword() {
        return Navigation.getSingletonObject().getPassword();
    }
    /**
     * Used for logging in
     * @param password the password reference that gets set when logging in
     */
    public void setPassword(String password) {
        Navigation.getSingletonObject().setPassword(password);
    }
    
 
    
}
